﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shell.DomainDot;
using Framework.Poco;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商视频的上下文
    /// </summary>
    public class AgencyVideoContext : DomainDotBase<Agency>
    {
        /// <summary>
        /// 属于经销商的视频
        /// </summary>
        public WithMany<Video> AgencyVideos { private get; set; }

        /// <summary>
        /// 所有视频
        /// </summary>
        public WithAll<Video> Videos { private get; set; }

        /// <summary>
        /// 获取所有视频
        /// </summary>
        public IEnumerable<Video> GetAllVideos()
        {
            return this.Videos;
        }

        /// <summary>
        /// 获取所有经销商的视频
        /// </summary>
        public IEnumerable<Video> GetAllAgencyVideos()
        {
            return this.AgencyVideos;
        }

        /// <summary>
        /// 根据一个ID获取一个视频
        /// </summary>
        public Video GetVideo(Guid identity)
        {
            return this.GetAllVideos().SingleOrDefault(v => v.Id == identity);
        }

        /// <summary>
        /// 创建属于经销商的视频
        /// </summary>
        public void Create(Video instance)
        {
            instance.Created = DateTime.Now;
            var entity = this.AgencyVideos.Create();
            PocoUtils.AssignProperty<Video>(instance, entity, typeof(Guid));
            this.AgencyVideos.Submit();
        }

        /// <summary>
        /// 删除属于经销商的视频
        /// </summary>
        public void Delete(Guid identity)
        {
            var original = this.GetAllAgencyVideos().SingleOrDefault(o => o.Id == identity);
            if (original != null)
            {
                this.AgencyVideos.Delete(identity);
                this.AgencyVideos.Submit();
            }
        }

        /// <summary>
        /// 修改属于经销商的视频
        /// </summary>
        public void Edit(Video instance)
        {
            var original = this.GetAllAgencyVideos().SingleOrDefault(o => o.Id == instance.Id);
            if (original != null)
            {
                original.Name = instance.Name;
                original.Description = instance.Description;
                original.VideoUrl = instance.VideoUrl;
                this.AgencyVideos.Submit();
            }
        }
    }
}
